package com.cooper.mediadb;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import com.cooper.scrapemedialib.R;
import com.cooper.scraper.ResourceScraper;
import com.cooper.scraper.ResourceScraperRunner;
import com.cooper.scraper.RunnerSubscribe;
import com.cooper.scraper.ScraperBrowseParam;
import com.cooper.scraper.show.ScraperParamPopMenu.OnParamSelectListener;

import android.content.Context;
import android.view.ViewGroup.LayoutParams;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class ParamRadioHelper implements RunnerSubscribe,
		OnCheckedChangeListener {

	private String scraperId;

	private int paramType;

	private Context context;

	private RadioGroup group;

	private OnParamSelectListener listener;

	private ResourceScraperRunner<?> runner;

	private ScraperBrowseParam useParam;

	public void init(Context context, RadioGroup group,
			ResourceScraperRunner<?> runner, String scraperId, int paramType) {
		this.group = group;
		this.scraperId = scraperId;
		this.paramType = paramType;
		this.context = context;
		this.runner = runner;
		runner.subscribe(this);
		runner.getBrowseParams(context, scraperId);
	}

	public OnParamSelectListener getListener() {
		return listener;
	}

	public void setListener(OnParamSelectListener listener) {
		this.listener = listener;
	}

	@Override
	public void onDownloadComplete(String key, Object data) {

		@SuppressWarnings("unchecked")
		List<ScraperBrowseParam> params = (List<ScraperBrowseParam>) data;

		useParam = null;
		for (ScraperBrowseParam param : params) {
			if (param.getType() == paramType) {
				useParam = param;
				break;
			}
		}
		if (useParam != null) {
			for (Entry<String, String> param : useParam.getParamList()
					.entrySet()) {

				RadioButton button = new RadioButton(context);
				button.setButtonDrawable(android.R.color.transparent);
				button.setBackgroundResource(R.drawable.bar_tab_radio);
				button.setText(param.getValue());
				button.setTag(param.getKey());
				int padding = context.getResources().getDimensionPixelSize(
						R.dimen.custom_bar_padding);
				button.setPadding(padding, padding, padding, padding);
		
				button.setOnCheckedChangeListener(this);
				group.addView(button, new RadioGroup.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
				if (useParam.getKey().equals(param.getKey())) {
					button.setChecked(true);
				}
			}
		}
		runner.cancelSubscribe(this, false);
	}

	@Override
	public void onDownloadFail(String key, int errorCode) {
		runner.cancelSubscribe(this, false);
	}

	@Override
	public void onDownloadCancel(String key) {
		runner.cancelSubscribe(this, false);
	}

	@Override
	public boolean subscribed(String key) {
		return key.equals(ResourceScraper.BROWSE_URL_PARAM_SUBSCRIBE_PREFIX
				+ scraperId);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		
		if (useParam != null && listener != null && isChecked) {
			List<ScraperBrowseParam> result = new ArrayList<ScraperBrowseParam>(
					1);
			useParam.setKey((String) buttonView.getTag());
			result.add(useParam);

			listener.onParamSelect(result);
		}
	}

}
